package com.monkey.tensquare.base.controller;

import com.monkey.tensquare.base.entity.City;
import com.monkey.tensquare.base.service.CityService;
import entity.PageResult;
import entity.Result;
import entity.StateCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import util.IdWorker;

import java.util.List;

@RestController
@RequestMapping("/city")
public class CityController {
    @Autowired
    private CityService cityService;
    @Autowired
    private IdWorker idWorker;
    @PostMapping
    public Result add(@RequestBody City city) {
        try {
            city.setId(idWorker.nextId() + "");
            cityService.add(city);
            return new Result(StateCode.OK, true, "添加成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StateCode.ERROR, false, "添加失败", null);
        }
    }
    @GetMapping
    public Result findAll() {
        try {
            List<City> cityList = cityService.findAll();
            return new Result(StateCode.OK, true, "查询所有城市列表成功", cityList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StateCode.ERROR, false, "查询所有城市列表失败", null);
        }
    }
    @PutMapping("{cityId}")
    public Result update(@PathVariable String cityId, @RequestBody City city) {
        try {
            cityService.update(cityId,city);
            return new Result(StateCode.OK, true, "修改成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StateCode.ERROR, false, "修改失败", null);
        }
    }
    @DeleteMapping("{cityId}")
    public Result delete(@PathVariable String cityId) {
        try {
            cityService.delete(cityId);
            return new Result(StateCode.OK, true, "删除成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StateCode.ERROR, false, "删除失败", null);
        }
    }
    @GetMapping("{cityId}")
    public Result findById(@PathVariable String cityId) {
        try {
            City city = cityService.findById(cityId);
            return new Result(StateCode.OK, true, "根据ID查询成功", city);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StateCode.ERROR, false, "根据ID查询失败", null);
        }
    }
    @PostMapping("search")
    public Result search(@RequestBody City city){
        try {
            List<City> cityList = cityService.search(city);
            return new Result(StateCode.OK, true, "搜索成功", cityList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StateCode.ERROR, false, "搜索失败", null);
        }
    }
    @PostMapping("search/{page}/{size}")
    public Result search(@RequestBody City city, @PathVariable Integer page, @PathVariable Integer size){
        try {
            PageResult<City> cityList = cityService.search(city,page,size);
            return new Result(StateCode.OK, true, "分页搜索成功", cityList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StateCode.ERROR, false, "分页搜索失败", null);
        }
    }
}
